// pages/reportCheck/reportCheck.js
const db = wx.cloud.database()
const _ = db.command
import { areaList } from '@vant/area-data';
Page({

  data: {
    stamp:440106,
    phone:"15876705385",
    currentDate:new Date().getTime(),
    minDate:new Date(2020, 11, 31).getTime(),
    maxDate: new Date(2099, 11, 31).getTime(),
    place:"",
    startTime:"",
    backTime:"",
    contentList:[],
    loading:false
  },
  zhanOrShou(e){
    const index=e.currentTarget.dataset.index
    this.setData({
      [`contentList[${index}].zhanshou`]:!this.data.contentList[index].zhanshou
    })
  },
  choosePlace(){
    this.animate('.area', [{
        height: 0
      },
      {
        height: "300px"
      }
    ], 110)
  },
  placeOut(){
    this.animate('.area', [{
      height: "300px"
    },
    {
      height: 0
    }
  ], 110)
  },
  placeComfirm(result){
    this.setData({
      ['place']:result.detail.values[0].name+'/'+result.detail.values[1].name+'/'+result.detail.values[2].name
    })
    this.placeOut()
  },
  chooseTime(e){  
    const Class=e.currentTarget.dataset.classname
    this.animate(Class, [{
      height: 0
    },
    {
      height: "300px"
    }
  ], 110)
  },
  timeComfirm(result){
    const time=result.currentTarget.dataset.time
    this.setData({
      [time]:`${new Date(result.detail.value).getFullYear()}/${new Date(result.detail.value).getMonth()+1}/${new Date(result.detail.value).getDate()}`
    })
    this.outTime()
  },
  outTime(){
    this.animate(".time", [
    {
      height: 0
    }
  ], 110)
  },
  
  onReachBottom(){
    this.getData()
  },
  onLoad(options) {
    // this.selectComponent(".password").show()
    this._index=0
    this.setData({
      areaList:areaList,
    })
    this.getData()
    
  },
  call(e){
    const phone=e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone 
    })
  },
  delete(e){
    const id=e.currentTarget.dataset.doc
    wx.showModal({
      title: '删除确认',
      content: '删除该条数据后将无法恢复',
      success: (res)=>{
        if (res.confirm) {
          db.collection("reportList").doc(id).remove({
            success:()=>{
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000,
              })
              this.resect()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
    
  },
  resect(){
    this._index=0
    this.setData({
      contentList:[],
      place:"",
      startTime:"",
      backTime:"",
    })

    this.getData()
  },
  search(){
    this._index=0
    this.setData({
      contentList:[],
    })
    this.getData()
  },
  getData(){
    this.setData({
      loading:true
    })
    let searchData={
      place:this.data.place,
      startTime:this.data.startTime,
      backTime:this.data.backTime
    }
    if(this.data.place===""){
      delete searchData.place
    }
    if(this.data.startTime===""){
      delete searchData.startTime
    }
    if(this.data.backTime===""){
      delete searchData.backTime
    }
    db.collection("reportList").where(searchData).skip(this._index*6).limit(6).get({
      success:(res)=>{
       if(res.data.length!==0){
        this.setData({
          contentList:this.data.contentList.concat(res.data)
         })
       }
       this.setData({
        loading:false
      })
       this._index++
      }
    })
  },
  nothing(){}

})